package com.ikas.ai.server.module.condition.controller;

import cn.hutool.core.date.StopWatch;
import com.alibaba.fastjson.JSONObject;
import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.aop.Pageable;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.condition.dto.WorkConditionConfigDTO;
import com.ikas.ai.server.module.condition.dto.WorkConditionFlowConfigDTO;
import com.ikas.ai.server.module.condition.dto.WorkStationAddDTO;
import com.ikas.ai.server.module.condition.dto.WorkStationUpdateDTO;
import com.ikas.ai.server.module.condition.enums.WorkFlowTypeEnum;
import com.ikas.ai.server.module.condition.service.WorkConditionFlowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工况配置列表
 */
@RestController
@RequestMapping("/work/condition")
@Slf4j
public class WorkConditionFlowController extends AbstractBean {

    @Autowired
    private WorkConditionFlowService workConditionFlowService;

    /**
     * 获取工况枚举
     *
     * @return
     */
    @GetMapping("/wcTypes")
    public JsonResult wcTypes() {
        return JsonResult.ok(JSONObject.toJSONString(WorkFlowTypeEnum.toMap()));
    }

    /**
     * 根据工况名字获取步骤细节
     */
    @GetMapping("/getStepsByWfName")
    public JsonResult getStepsByWfName(String machineNo, String wfName) {
        return JsonResult.ok(workConditionFlowService.getStepsByWfName(machineNo, wfName));
    }

    /**
     * 根据名称获取工况模板数据 eg: wc_name = TB26
     *
     * @param wcName
     * @return
     */
    @GetMapping("/findWorkConditionFlow/{machine_no}/{wc_name}")
    public JsonResult findWorkConditionFlow(@PathVariable("machine_no") String machineNo, @PathVariable("wc_name") String wcName) {
        return JsonResult.ok(workConditionFlowService.findWorkConditionFlowConfiguration(machineNo, wcName));
    }

    /**
     * 根据机组获取工况流程配置分页列表
     *
     * @param machineNo
     * @return
     */
    @GetMapping("/page")
    @Pageable(sortType = "desc")
    public JsonResult page(@RequestParam(name = "machineNo", required = false) String machineNo) {
        return JsonResult.ok(workConditionFlowService.getPage(machineNo));
    }

    /**
     * 机组工况信息配置
     *
     * @param workConditionConfigDTO
     * @return
     */
    @PostMapping("/info/config")
    public JsonResult infoConfig(@RequestBody WorkConditionConfigDTO workConditionConfigDTO) {
        workConditionFlowService.infoConfig(workConditionConfigDTO);
        return JsonResult.ok();
    }

    /**
     * 机组工况流程json配置
     *
     * @param workConditionFlowConfigDTO
     * @return
     */
    @PostMapping("/flow/config")
    public JsonResult flowConfig(@RequestBody WorkConditionFlowConfigDTO workConditionFlowConfigDTO) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        workConditionFlowService.flowConfig(workConditionFlowConfigDTO);
        stopWatch.stop();
        log.info("flow-config执行时长：{}", stopWatch.getTotalTimeSeconds());
        return JsonResult.ok();
    }

    /**
     * 根据工况流程id获取配置详情
     *
     * @param id
     * @return
     */
    @GetMapping("/flow/config/info/{id}")
    public JsonResult flowConfigInfo(@PathVariable("id") Long id) {
        return JsonResult.ok(workConditionFlowService.flowConfigInfo(id));
    }


    /**
     * 停用启用工况配置
     *
     * @param id
     * @param state
     * @return
     */
    @GetMapping("/state/{id}/{state}")
    public JsonResult state(@PathVariable("id") Long id, @PathVariable("state") Integer state) {
        workConditionFlowService.state(id, state);
        return JsonResult.ok();
    }

    /**
     * 获取厂站分页列表
     *
     * @param machineNo
     * @param stationNo
     * @return
     */
    @GetMapping("/stationPage")
    @Pageable(sortType = "asc")
    public JsonResult stationPage(@RequestParam(value = "machineNo", required = false) String machineNo,
                                  @RequestParam(value = "stationNo", required = false) String stationNo) {
        return JsonResult.ok(workConditionFlowService.stationPage(machineNo, stationNo));
    }

    /**
     * 根据机组编号和工况名获取详情
     *
     * @param machineNo
     * @param wfName
     * @return
     */
    @GetMapping("/detail")
    public JsonResult detail(@RequestParam(value = "machineNo") String machineNo,
                             @RequestParam(value = "wfName") String wfName) {
        return JsonResult.ok(workConditionFlowService.detail(machineNo, wfName));
    }

    /**
     * 获取厂站列表
     *
     * @return
     */
    @GetMapping("/stationList")
    public JsonResult stationList() {
        return JsonResult.ok(workConditionFlowService.stationList());
    }

    /**
     * 新增厂站
     *
     * @param workStationAddDTO
     * @return
     */
    @PostMapping("/addStation")
    public JsonResult addStation(@RequestBody @Validated WorkStationAddDTO workStationAddDTO) {
        workConditionFlowService.addStation(workStationAddDTO, getUser());
        return JsonResult.ok();
    }

    /**
     * 更新厂站
     *
     * @param workStationUpdateDTO
     * @return
     */
    @PostMapping("/updateStation")
    public JsonResult updateStation(@RequestBody @Validated WorkStationUpdateDTO workStationUpdateDTO) {
        workConditionFlowService.updateStation(workStationUpdateDTO, getUser());
        return JsonResult.ok();
    }

    /**
     * 批量删除厂站
     *
     * @param ids
     * @return
     */
    @GetMapping("/deleteStation")
    public JsonResult deleteStation(@RequestParam("ids") List<Long> ids) {
        workConditionFlowService.deleteStation(ids, getUser());
        return JsonResult.ok();
    }

}
